package com.example.demo2.service.impl;

import com.example.demo2.mapper.CategoryMapper;
import com.example.demo2.pojo.PageBean;
import com.example.demo2.utils.DataUtils;
import com.example.demo2.utils.Result;
import com.example.demo2.pojo.data.CategoryDO;
import com.example.demo2.pojo.vo.CategoryVO;
import com.example.demo2.service.CategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/** 
 * <br/>
 * Created by weidong on 2020/02/16
 */
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {

	@Resource
	private CategoryMapper categoryMapper;

	@Override 	public Result add(CategoryVO categoryVO) throws Exception {
		Integer count = categoryMapper.add(categoryVO);
		if (count > 0) {
			return Result.getSuccess("添加成功");
		}else{
			return Result.getSuccess("添加失败");
		}
	}

	@Override
	public Result updateById(CategoryVO categoryVO) throws Exception {
		Integer count = categoryMapper.updateById(categoryVO);
		if (count > 0) {
			return Result.getSuccess("操作成功");
		}else{
			return Result.getSuccess("操作失败");
		}
	}
	
	@Override
	public Result findById(Integer id) throws Exception{
		CategoryDO categoryDO = categoryMapper.findById(id);
		if (categoryDO != null) {
            System.out.println(categoryDO);
			Result result = new Result();
			result.setMsg("操作成功");
			result.setData(categoryDO);
			result.setCode(Result.CODE_SUCCESS);
			return result;
		}else{
			return Result.getFailure("操作失败");
		}
	}

	@Override
	public Result list(CategoryVO categoryVO) throws Exception{
		List<CategoryDO> categoryDOs = categoryMapper.list(categoryVO);
		if (categoryDOs != null) {
			Result result = new Result();
			result.setMsg("查询成功");
			result.setData(categoryDOs);
			result.setCode(Result.CODE_SUCCESS);
			return result;
		}else{
			return Result.getFailure("操作失败");
		}
	}
	int pageSize=10;
	int navigatePages=3;
	@Override
	public PageBean listPage(CategoryVO categoryVO) throws Exception{
		//设置分页数据大小
		categoryVO.setPageSize(pageSize);
		//查询数目的总个数
		Integer count=categoryMapper.count(categoryVO);
		if(count==0){
			PageBean pageBean=new PageBean(null,pageSize,categoryVO.getCurrentPage(),count,navigatePages);
			//数据库没有数据
			return pageBean;
		}

		//判断页码是否越界,如果页码大于最大值,返回最后一页数据
		//计算最大页码数
		int max= DataUtils.getPageCount(count,pageSize);
		if(categoryVO.getCurrentPage()>max){
			categoryVO.setCurrentPage(max);
		}

		//设置查询起始位置
		categoryVO.setStartRecord(DataUtils.getStartRecord(categoryVO.getCurrentPage(),pageSize));

		//调用mapper查询数据
		List<CategoryDO> flatsDOS=categoryMapper.listPage(categoryVO);
		PageBean pageBean=new PageBean(flatsDOS,pageSize,categoryVO.getCurrentPage(),count,navigatePages);

		return pageBean;
	}

	
	@Override
	public Result count(CategoryVO categoryVO) throws Exception{
		return null;
	}
	
	@Override
	public Result deleteById(Integer id) throws Exception{
		Integer count = categoryMapper.deleteById(id);
		if (count > 0) {
			return Result.getSuccess("添加成功");
		}else{
			return Result.getFailure("操作失败");
		}
	}

	@Override
	public Result deleteByCategoryId(Integer id) throws Exception {
		Integer count = categoryMapper.deleteById(id);
		if (count > 0) {
			return Result.getSuccess("删除成功");
		}else{
			return Result.getFailure("删除失败");
		}
	}

	@Override
	public CategoryDO findCategoryName(Integer id) throws Exception {
		return categoryMapper.findById(id);
	}

}